import { defineStore } from "pinia";
import { useWebstock } from '@/store';
import { notification } from 'ant-design-vue';
import { admin_patinetList, patinetList, getltemTopic } from "@/api/patient";

export const useWorkBenches = defineStore('workbenches', {
  state: () => {
    return {
      workerPatientList:[],
      poplist:[],
	  swipeList:[],
	  currentAnswer:{},
	  workAsideList:{},
	  currentQuest:[],
	  currentSort:0,
	  Instvisible:true,
	  isRouter:false
    }
  },
  actions: {
   async getList(isRead = false){ // 得到自己房间的患者
       if(isRead || this.workerPatientList.length == 0){
		   const res = await admin_patinetList({ page:1,size:100 });
		   if(!res) return;
		   const { list, total } = res;
		   if(list){
			   this.workerPatientList = list;
			   const currentId = localStorage.getItem('currentId');
			   if(!currentId){
				   localStorage.setItem('currentId', list[0].id);
			   }
		   }else{
			   this.workerPatientList = [];
		   }
	   }
    },
	async getpopList(isRead = false){ // 得到所有患者
		const res = await patinetList({ page:1,size:100 });
	    if(isRead || res){
			const { list, total } = res;
			if(list){
				this.poplist = list;
			}else{
				this.poplist = [];
			}
		}
	},
	async setSwipeList(arr){
		this.swipeList = arr;
		useWebstock().sendMessage({ messageCode:'6'});
		
	},
	changePatientList(data){ // 举手帮助
		const { userId,messageData } = data;
		this.workerPatientList = this.workerPatientList.map((item)=> ({ ...item,status:item.id == userId?3:item.status }));
		notification.open({
		    message: messageData,
			style:{
				border:'2px solid #f69c55',
				borderRadius:'7px'
			},
			// duration:null,
		    // description:
		    //       'This is the content of the notification. This is the content of the notification. This is the content of the notification.',
		    onClick: () => {
				// this.workerPatientList = this.workerPatientList.map((item)=> ({ ...item,status:item.id == userId?2:item.status }));
		        console.log('Notification Clicked!');
		    },
		});
	},
	submitList(data){ // 提交答案
		const { userId, userAnswer } = data;
		const currentId = localStorage.getItem('currentId');
		// console.log(data, '提交答案')
		if(currentId == userId){
			this.currentAnswer = data;
		}else{
			this.workerPatientList = this.workerPatientList.map((item)=> ({ ...item,answer:data }));
		}
	},
	getstep(){
		this.currentSort++;
		this.currentAnswer = {};
	},
	setworkAsideList(data){
		this.workAsideList  = data;
	},
	setInstvisible(data){
		this.Instvisible  = data;
	},
	isRouterFn(){ // 点击开始测评后的返回跳转路由
		this.isRouter = true;
	},
	async setCurrentQuest(data){
		// const currentId = localStorage.getItem('currentId');
		// const arr  = data.filter((item)=> item.userId == currentId);
		// console.log(data, "arr");
		// if(arr.length > 0){
		// 	// const { userAnswer, itemTopicId } = arr[0];
		// 	// console.log(userAnswer,'xx');
		// 	if(userAnswer){
		// 		this.currentAnswer = userAnswer;
				const { itemTopicId } = data[0];
				const res = await getltemTopic({ itemTopicId });
				if(!res) return;
				const { userItemRecordAnswer } = res;
				this.userAnswer = userItemRecordAnswer;
				this.currentQuest = { ...res,...data[0] };
		// 		if(!res) return;
		// 		// console.log(arr[0])
		// 		// const { id } = res;
		// 		this.currentQuest = { ...res,...arr[0] };
		// 		// console.log(res,"userAnswer")
		// 	}
		// }else{
		// 	this.currentAnswer = [];
		// }
		// console.log(arr[])
	}
  },
  getters: {
  },
  // persist: true
})
